package org.leumasjaffe.initiativeTracker.event;


import java.awt.event.ActionEvent;

import javax.swing.Action;

import org.leumasjaffe.initiativeTracker.Constants;
import org.leumasjaffe.initiativeTracker.entity.PC;
import org.leumasjaffe.initiativeTracker.operator.CharacterLinker;
import org.leumasjaffe.initiativeTracker.view.ControlPanel;

import org.leumasjaffe.util.io.PortableScanner;
import edu.wpi.heineman.xml.Message;
import edu.wpi.heineman.client.ServerAccess;

@SuppressWarnings("serial")
public class DamageAction extends InitiativeTrackerAction implements Action {
	
	public DamageAction (String text) {
		super(text);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		String xmlString = Message.requestHeader() + "<damageRequest game='" + Constants.id + "'>";
		
		int dmg = ControlPanel.getInstance().getDamage();
		
		for (int i = 0; i < list.size(); i++) {
			CharacterLinker c = list.get(i);
			if (c.getCharPanel().isTarget()) {
				boolean temp = false;
				if (dmg < 0)
					if (c.getCharacter() instanceof PC) {
						System.out.print("Is this temp hp: ");
						String s = PortableScanner.nextLine();
						if (s.equalsIgnoreCase("y") || s.equalsIgnoreCase("yes"))
							temp = true;
					}
				xmlString += "<target id='" + c.getCharacter().getId() + "' value='"
							+ dmg + "' temp='" + temp + "'/>";
				if (c.damage(dmg, temp)) {
					c.tearDown();
					root.addDeath(i);
					list.remove(c);
					i--;
					c = null;
				}
			}
		}
		
		xmlString += "</damageRequest></request>";
		if (xmlString.contains("target"))
			ServerAccess.getInstance().sendRequest(new Message(xmlString));
		
		root.rebuild();
		gui.repaint();
	}
	
}
